package com.ljkj.blockpay.basedata;

import android.app.Activity;
import android.text.TextUtils;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 所有activity管理
 */
public class ActivityCollector {
    public static List<WeakReference<Activity>> activities = new ArrayList<>();

    public static void addActivity(Activity activity) {
        activities.add(new WeakReference<>(activity));
    }

    public static void removeActivity(Activity activity) {
        for (WeakReference<Activity> activityRef : activities) {
            if (activityRef.get() != null && activityRef.get() == activity) {
                activities.remove(activityRef);
                break;
            }
        }
    }

    public static void finishAll() {
        for (WeakReference<Activity> activityRef : activities) {
            Activity activity = activityRef.get();
            if (activity != null) {
                activity.finish();
            }
        }
    }

    public static Activity findMainActivity() {
        Activity mainPageActivity = null;
        for (WeakReference<Activity> activityRef : activities) {
            Activity activity = activityRef.get();
            if (activity == null) {
                continue;
            }
            String activityPackageName = activity.getLocalClassName();
            if (TextUtils.isEmpty(activityPackageName) || !"com.viva.app.toc.ui.activity.MainActivity".equals(activityPackageName)) {
                continue;
            }
            mainPageActivity = activity;
        }
        return mainPageActivity;
    }

    public static boolean isAllActivityFinished() {
        return activities.size() == 0;
    }
}
